package com.ynyr.jar.wss.service.impl;

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.ynyr.jar.common.jpa.util.QueryDSLUtil;
import com.ynyr.jar.wss.entity.QWssEUser;
import com.ynyr.jar.wss.entity.WssEUser;
import com.ynyr.jar.wss.repository.WssEUserRepository;
import com.ynyr.jar.wss.service.IWssEUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.config.annotation.authentication.configurers.provisioning.UserDetailsManagerConfigurer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Service
public class WssEUserServiceImpl implements IWssEUserService, UserDetailsService {

    private final JPAQueryFactory factory;

    private final WssEUserRepository wssEUserRepository;

    public WssEUserServiceImpl(JPAQueryFactory factory, WssEUserRepository wssEUserRepository) {
        this.factory = factory;
        this.wssEUserRepository = wssEUserRepository;
    }

    @Transactional
    @Override
    public void save(WssEUser user) {
        wssEUserRepository.save(user);
    }

    @Transactional
    @Override
    public void deleteByWssEUserId(String wssEUserId) {
        QWssEUser qWssEUser = QWssEUser.wssEUser;
        factory.delete(qWssEUser)
                .where(qWssEUser.wssEUserId.eq(wssEUserId));
    }

    @Transactional
    @Override
    public void deleteByWssEUserIds(String... wssEUserIds) {
        QWssEUser qWssEUser = QWssEUser.wssEUser;
        factory.delete(qWssEUser)
                .where(qWssEUser.wssEUserId.in(wssEUserIds));
    }

    @Transactional
    @Override
    public void modifyByWssEUserId(WssEUser wssEUser) {
        QWssEUser qWssEUser = QWssEUser.wssEUser;
        WssEUser realUser = factory.selectFrom(qWssEUser)
                .where(qWssEUser.wssEUserId.eq(wssEUser.getWssEUserId()))
                .fetchOne();
        realUser.setNickname(wssEUser.getNickname());
        wssEUserRepository.save(realUser);
    }

    @Override
    public WssEUser findByWssEUserId(String wssEUserId) {
        QWssEUser qWssEUser = QWssEUser.wssEUser;
        WssEUser realUser = factory.selectFrom(qWssEUser)
                .where(qWssEUser.wssEUserId.eq(wssEUserId))
                .fetchOne();
        return realUser;
    }

    @Override
    public WssEUser findByUsernameAndPassword(String username, String password) {
        QWssEUser qWssEUser = QWssEUser.wssEUser;
        return factory.selectFrom(qWssEUser)
                .where(qWssEUser.username.eq(username).and(
                        qWssEUser.password.eq(password)
                )).fetchOne();
    }

    @Override
    public Page<WssEUser> findAll(Pageable pageable) {
        QWssEUser qWssEUser = QWssEUser.wssEUser;
        QueryDSLUtil.page(factory.selectFrom(qWssEUser).orderBy(qWssEUser.createDate.desc()), pageable);
        return null;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QWssEUser qWssEUser = QWssEUser.wssEUser;
        WssEUser realUser = factory.selectFrom(qWssEUser)
                .where(qWssEUser.username.eq(username))
                .fetchOne();

        if(Objects.isNull(realUser)) {
            throw new UsernameNotFoundException(String.format("[%s]用户不存在", username));
        }

        // 返回一个简单的 UserDetails, 并且
        return  User.withUsername(username)
                .password(realUser.getPassword())
                .authorities(new SimpleGrantedAuthority("simple"))
                .build();
    }
}
